package com.jianggy.adapter.springmvc;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author jianggy
 * @time 2018年11月13日 下午2:17:37
 * @description
 */
public class DispatcherServlet {
	List<HandlerAdapter> adapters = new ArrayList<>();

	public void init() {
		adapters.add(new SimpleControllerHandlerAdapter());
		adapters.add(new HttpServletHandlerAdapter());
	}

	public void serivce(String url) {
		String response = this.doPatcher(url);
		System.out.println("返回的结果为："+response);
	}

	public String doPatcher(String url) {
		HandlerMapping mapping = new HandlerMapping();
		Object object = mapping.lookHandler(url);
		String response = null;
		for (Iterator iterator = adapters.iterator(); iterator.hasNext();) {
			HandlerAdapter handlerAdapter = (HandlerAdapter) iterator.next();
			if (handlerAdapter.support(object)) {
				response = handlerAdapter.handler();
			}
		}
		return response;
	}
}
